﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace fixs
{
    class Sessions
    {
        public const int cses = 10; //max clients count
        // [session] [ip] [hostname] [clientversion]
        static public string[,] sess = new string[cses, 4];

        static public int sIsRangeMas()
        {
            int ind =-1;
            if (cses == 0)
            {
                Console.WriteLine("[sSetNullTable] In Table rows = 0. Not edit table.");
                return ind;
            }
            else
            {
                for (int i = 0; i < cses; ++i)
                    if (sess[i, 0] == "(none)")
                    {
                        ind = i;
                        break;
                    }
                return ind;
            }
        }

        static public void sSetNullTable()
        {
            if (cses == 0) Console.WriteLine("[sSetNullTable] In Table rows = 0. Not edit table."); //error
            else
            {
                for (int i = 0; i < cses; ++i)
                    for (int j = 0; j < 4; ++j)
                        sess[i, j] = "(none)";
                Console.WriteLine("[sSetNullTable] All elements in Table set Null."); //debug
            }
        }

        static public bool sAddSes(string s, string ip, string hn, string cv)
        {
            if (sIsRangeMas()==-1)
            {
                Console.WriteLine("[sAddSes] In array not null elements."); //error
                return false;
            }
            else
            {
                int ind = sIsRangeMas();
                sess[ind, 0] = s;
                sess[ind, 1] = ip;
                sess[ind, 2] = hn;
                sess[ind, 3] = cv;
                Console.WriteLine("[sAddSes] Session added."); //debug
                return true;
            }
        }

        static public bool sDelSes(string s)
        {
            if (cses == 0)
            {
                Console.WriteLine("[sDelSes] In Table rows = 0. Not edit table.");
                return false;
            }
            else
            {
             int ind = -1;
             for(int i=0;i<cses;++i)
                 if (sess[i, 0] == s)
                 {
                     for (int j = 0; j < 4; ++j) sess[i, j] = "(none)";
                     ind = i;
                 }
             if (ind == -1)
             {
                 Console.WriteLine("[sDelSes] Session not find in the Table. Not deleted."); //debug
                 return false;
             }
             else
             {
                 Console.WriteLine("[sDelSes] Session deleted.");
                 return true;
             }
            }
          
        }

        static public void sWriteTable()
        {
            Console.WriteLine("Таблица сессий:"); //info
            Console.WriteLine("[session] [ip] [hostname] [clientversion]"); //info
            for (int i = 0; i < cses; ++i)
            {
                if (sess[i, 0] == "(none)") continue;
                for (int j = 0; j < 4; ++j) Console.Write("[" + sess[i, j] + "] "); //info
                Console.WriteLine();
            }
        }
        
    }
}
